package org.cancer.hadesdata.service;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import org.cancer.hadesdata.dao.FileTypeDao;
import org.cancer.hadesdata.exception.HadesException;
import org.cancer.hadesdata.exception.SynoException;
import org.cancer.hadesdata.model.FileType;
import org.cancer.hadesdata.syno.SynoManager;
import org.cancer.hadesdata.syno.SynoManager.DownloadMode;
import org.cancer.hadesdata.syno.SynoManager.ImageSize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

@Service
public class FileService {

	@Autowired
	private SynoManager synoManager;
	@Autowired
	private FileTypeDao fileTypeDao;

	public List<FileType> findComposites(String parentId) {
		return this.findComposites(parentId, 0);
	}
	
	@Transactional
	public List<FileType> findComposites(String parentId, int deep) {
		List<FileType> composites = Lists.newArrayList();
		List<FileType> list = Lists.newArrayList();
		FileType parent = this.findById(parentId);
		try {
			if (parent == null)
				throw new HadesException("null File info:" + parentId);
			String parentUri = parent.getUrl();
			composites = synoManager.list(parentUri);
		} catch (SynoException e) {
			if (!e.getErrCode().equals("408"))
				e.printStackTrace();
		} catch (HadesException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (composites.size() > 0) {
			Iterator<FileType> it = composites.iterator();
			while (it.hasNext()) {
				FileType file = it.next();
				List<FileType> fileList = fileTypeDao.findByUrl(file.getUrl());
				if (fileList != null && fileList.size() == 1) {
					file = fileList.get(0);
				} else {
					if (fileList != null && fileList.size() > 1) {
						fileTypeDao.removeByUrl(file.getUrl());
					}
					file.setId(UUID.randomUUID().toString().replaceAll("-", ""));
					fileTypeDao.save(file);
				}
				if (deep > 0 || deep == -1) {
					file.setComposites(this.findComposites(file.getId(), deep - 1));
				}
				list.add(file);
			}
		}
		return list;
	}

	public FileType findById(String fileId) {
		FileType file = null;
		if (fileId != null) {
			Optional<FileType> opt = fileTypeDao.findById(fileId);
			if (opt.isPresent())
				file = opt.get();
		}
		if (file == null) {
			file = new FileType();
			file.setId(fileId);
		} else {
			file.setThumbUrl(synoManager.image(file.getUrl(), ImageSize.SMALL));
			file.setViewUrl(synoManager.image(file.getUrl(), ImageSize.LARGE));
			file.setOpenUrl(synoManager.download(file.getUrl(), DownloadMode.OPEN));
			file.setDownloadUrl(synoManager.download(file.getUrl(), DownloadMode.DOWNLOAD));
		}
		return file;
	}

	public FileType findByUrl(String fileUrl) {
		FileType file = null;
		if (fileUrl != null) {
			List<FileType> opt = fileTypeDao.findByUrl(fileUrl);
			if (opt != null)
				file = opt.get(0);
		}
		if (file == null) {
			file = new FileType();
			file.setUrl(fileUrl);
		} else {
			file.setThumbUrl(synoManager.image(file.getUrl(), ImageSize.SMALL));
			file.setViewUrl(synoManager.image(file.getUrl(), ImageSize.LARGE));
			file.setOpenUrl(synoManager.download(file.getUrl(), DownloadMode.OPEN));
			file.setDownloadUrl(synoManager.download(file.getUrl(), DownloadMode.DOWNLOAD));
		}
		return file;
	}

	public String image(String fileId, ImageSize size) {
		FileType file = this.findById(fileId);
		String out = synoManager.image(file.getUrl(), size);
		return out;
	}

	public String download(String fileId) {
		FileType file = this.findById(fileId);
		String out = synoManager.download(file.getUrl(), DownloadMode.DOWNLOAD);
		return out;
	}

	public String open(String fileId) {
		FileType file = this.findById(fileId);
		String out = synoManager.download(file.getUrl(), DownloadMode.OPEN);
		return out;
	}
}
